package com.ylbh.modules.shopping_orderform.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Query;
import com.ylbh.modules.shopping_orderform.dao.ShoppingOrderformDao;
import com.ylbh.modules.shopping_orderform.entity.ShoppingOrderformEntity;
import com.ylbh.modules.shopping_orderform.entity.entityVo.ShoppingOrderformVo;
import com.ylbh.modules.shopping_orderform.service.ShoppingOrderformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Query;

@Service("shoppingOrderformService")
public class ShoppingOrderformServiceImpl extends ServiceImpl<ShoppingOrderformDao, ShoppingOrderformEntity> implements ShoppingOrderformService {

//    private final String uploadShoppingGoodsSpec = "https://app-store-goods.oss-cn-beijing.aliyuncs.com/consumer/";
    private final String uploadShoppingGoodsSpec = "https://image.yilianbaihui.cn/consumer/";
    @Autowired
    ShoppingOrderformDao shoppingOrderformDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShoppingOrderformEntity> page = this.page(
                new Query<ShoppingOrderformEntity>().getPage(params),
                new QueryWrapper<ShoppingOrderformEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils orderlist(Map<String, Object> params) {
        QueryWrapper<ShoppingOrderformVo.orderListVo> wrapper = new QueryWrapper<>();
        Integer page1 = Integer.parseInt(params.get("page").toString());
        Integer limit = Integer.parseInt(params.get("limit").toString());
        Page<ShoppingOrderformVo.orderListVo> pageL = new Page<ShoppingOrderformVo.orderListVo>(page1,limit);
        List<ShoppingOrderformVo.orderListVo> orderListVos = shoppingOrderformDao.orderList(params,pageL);
        for (ShoppingOrderformVo.orderListVo vo:orderListVos) {
            if(null != vo.getAcName()){
                String imgSrc = uploadShoppingGoodsSpec +vo.getAcPath()+"/"+vo.getAcName();
                vo.setImgSrc(imgSrc);
            }else{
                String imgSrc = uploadShoppingGoodsSpec +vo.getAcPath();
                vo.setImgSrc(imgSrc);
            }
            if (null != vo.getDeliveryaddress()){
                String[] deliveryAddresses = vo.getDeliveryaddress().split(",,,,,,");
                if (deliveryAddresses.length == 3) {
                    vo.setConsignee(deliveryAddresses[0]);
                    vo.setPhone(deliveryAddresses[1]);
                    vo.setAddress(deliveryAddresses[2]);
                } else {
                    vo.setConsignee("");
                    vo.setPhone("");
                    vo.setAddress("");
                }
            }
            vo.setGoodId("http://www.yilianbaihui.cn/item_show.html?goodsId="+vo.getGoodId());
        }
        Page<ShoppingOrderformVo.orderListVo> page = new Page<>();
        page.setRecords(orderListVos);
        page.setTotal(pageL.getTotal());
        page.setTotal(pageL.getTotal());
        return new PageUtils(page);
    }

}
